package com.mec.ServerCluser.RPC;

import java.lang.reflect.Method;

/**
 * Rpc方法注册到RpcBeanFactory中的具体过程，以及多种注册方式的重载。
 */
public class RpcBeanRegistry {
	RpcBeanRegistry() {
	}
	
	static void registInterface(RpcBeanFactory rpcBeanFactory, Class<?> interfaces) {
		doRegist(rpcBeanFactory, interfaces, null);
	}

	private static void doRegist(RpcBeanFactory rpcBeanFactory, Class<?> interfaces, Object object) {
		Method[] methods = interfaces.getDeclaredMethods();
		for (Method method : methods) {
			String rpcBeanId = String.valueOf(method.toString().hashCode());
			//识别码的计算
			RpcBeanDefination rpcBeanDefination = new RpcBeanDefination();
			rpcBeanDefination.setKlass(interfaces);
			rpcBeanDefination.setMethod(method);
			rpcBeanDefination.setObject(object);
			
			rpcBeanFactory.rpcBeanRegistry(rpcBeanId, rpcBeanDefination);
		}
	}
	
	static void registInterface(RpcBeanFactory rpcBeanFactory, Class<?> interfaces, Object object) {
		if (!interfaces.isAssignableFrom(object.getClass())) {
			return;
		}
		doRegist(rpcBeanFactory, interfaces, object);
	}
	
	static void registInterface(RpcBeanFactory rpcBeanFactory, Class<?> interfaces, Class<?> klass) {
		if (!interfaces.isAssignableFrom(klass)) {
			return;
		}
		try {
			doRegist(rpcBeanFactory, interfaces, klass.newInstance());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
